package myself.a011HowAreYou;

import java.util.PriorityQueue;

/**
 * @author 叶之越
 * Description 投资问题
 * Date 2021/1/30
 * Time 22:05
 * Mail 739153436@qq.com
 */
public class IPO {

    /**
     * costs[i]表示i号项目的花费
     * profits[i]表示i号项目在扣除花费之后还能挣到的钱(利润)
     * K表示你只能串行的最多做k个项目
     * M表示你初始的资金
     */
    public static int findMaximizedCapital(int K, int M, int[] profits, int[] cost) {
        Program[] arr = new Program[profits.length];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new Program(cost[i], profits[i]);
        }
        PriorityQueue<Program> costHeap = new PriorityQueue<>((o1, o2) -> o1.c - o2.c);
        PriorityQueue<Program> profitHeap = new PriorityQueue<>((o1, o2) -> o2.p - o2.p);
        for (Program program : arr) {
            costHeap.add(program);
        }
        for (int i = 0; i < K; i++) {
            while (!costHeap.isEmpty() && costHeap.peek().c < M) {
                profitHeap.add(costHeap.poll());
            }
            if (profitHeap.isEmpty()) {
                return M;
            }
            M += profitHeap.poll().p;
        }

        return M;
    }

    public static class Program {
        // profit 利润
        public int p;
        // cost 成本
        public int c;

        public Program(int p, int c) {
            this.p = p;
            this.c = c;
        }
    }
}
